1 Data Collapse

1.1 Tech and Risk Tier Pass

1.2 Risk Tier Pass, Tech Tier Fail

1.3 Tech Tier Pass, Risk Tier Fail

1.4 No quality filters

2 SSD Build (Tech and Risk Tier Pass)

2.1 Tier 1

2.1.1 Large (10 - 5000 um) (10 - 5000 um)

2.1.1.1 Tier1_large_median

tier1_large_median_ggplot <- ggplot(pred_tier1_large_median,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_tier1_large_median,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_tier1_large_median, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "tier1_large_median") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
tier1_large_median_ggplot

2.1.1.2 tier1_large_mean

tier1_large_mean_ggplot <- ggplot(pred_tier1_large_mean,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_tier1_large_mean,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_tier1_large_mean, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "tier1_large_mean") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
tier1_large_mean_ggplot

2.1.1.3 tier1_large_1q

2.1.1.4 tier1_large_min

2.1.2 Small (1-10 um)

2.1.2.1 Tier1_small_median

2.1.2.2 tier1_small_mean

2.1.2.3 tier1_small_1q

2.1.2.4 tier1_small_min

2.2 tier 2

2.2.1 Large (10 - 5000 um)

2.2.1.1 tier2_large_median

tier2_large_median_ggplot <- ggplot(pred_tier2_large_median,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_tier2_large_median,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_tier2_large_median, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "tier2_large_median") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
tier2_large_median_ggplot

2.2.1.2 tier2_large_mean

tier2_large_mean_ggplot <- ggplot(pred_tier2_large_mean,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_tier2_large_mean,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_tier2_large_mean, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "tier2_large_mean") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
tier2_large_mean_ggplot

2.2.1.3 tier2_large_1q

2.2.1.4 tier2_large_min

2.2.2 Small (1-10 um)

2.2.2.1 tier2_small_median

2.2.2.2 tier2_small_mean

2.2.2.3 tier2_small_1q

2.2.2.4 tier2_small_min

2.3 Tier 3

2.3.1 Large (10 - 5000 um)

2.3.1.1 tier3.4_large_median

tier3.4_large_median_ggplot <- ggplot(pred_tier3.4_large_median,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_tier3.4_large_median,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_tier3.4_large_median, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "tier3.4_large_median") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
tier3.4_large_median_ggplot

2.3.1.2 tier3.4_large_mean

tier3.4_large_mean_ggplot <- ggplot(pred_tier3.4_large_mean,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_tier3.4_large_mean,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_tier3.4_large_mean, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "tier3.4_large_mean") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
tier3.4_large_mean_ggplot

2.3.1.3 tier3.4_large_1q

2.3.1.4 tier3.4_large_min

2.3.2 Small (1-10 um)

2.3.2.1 tier3.4_small_median

2.3.2.2 tier3.4_small_mean

2.3.2.3 tier3.4_small_1q

2.3.2.4 tier3.4_small_min

3 Combine plots

3.1 Tier 1

3.1.1 Small (1-10 um)s

### tier1_small_median
ggplot(pred_tier1_small_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_tier1_small_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: tier1_small_mean ###
  geom_xribbon(data = pred_tier1_small_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_tier1_small_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_tier1_small_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: tier1_small_1q ###
    geom_xribbon(data = pred_tier1_small_1q, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_tier1_small_1q, aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_tier1_small_1q, aes(x = Conc, y =frac), 
                 color = "green") +  
  
### fourth line: tier1_small_min ###
    geom_xribbon(data = pred_tier1_small_min, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_tier1_small_min, aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_tier1_small_min, aes(x = Conc, y =frac), 
                 color = "grey10") +  
### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (Tier 1)",
       subtitle = ("Size Range: 1 - 10 um; Red = median, blue = mean, green = first quartile, grey = minimum"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

### Large (10 - 5000 um)s

### tier1_large_median
ggplot(pred_tier1_large_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_tier1_large_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: tier1_large_mean ###
  geom_xribbon(data = pred_tier1_large_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_tier1_large_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_tier1_large_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: tier1_large_1q ###
    geom_xribbon(data = pred_tier1_large_1q, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_tier1_large_1q, aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_tier1_large_1q, aes(x = Conc, y =frac), 
                 color = "green") +  
  
  ### fourth line: tier1_large_min ###
    geom_xribbon(data = pred_tier1_large_min, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_tier1_large_min, aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_tier1_large_min, aes(x = Conc, y =frac), 
                 color = "grey10") +  
### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (Tier 1)",
       subtitle = ("Size Range: 10 - 5,000 um; Red = median, blue = mean, green = first quartile, grey = minimum"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

3.2 Tier 2

3.2.1 Small (1-10 um)s

### tier2_small_median
ggplot(pred_tier2_small_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_tier2_small_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: tier2_small_mean ###
  geom_xribbon(data = pred_tier2_small_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_tier2_small_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_tier2_small_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: tier2_small_1q ###
    geom_xribbon(data = pred_tier2_small_1q, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_tier2_small_1q, aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_tier2_small_1q, aes(x = Conc, y =frac), 
                 color = "green") +  
  ### fourth line: tier2_small_min  ###
    geom_xribbon(data = pred_tier2_small_min , aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_tier2_small_min , aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_tier2_small_min , aes(x = Conc, y =frac), 
                 color = "grey10") +  
  ### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (Tier 2)",
       subtitle = ("Size Range: 1 - 10 um; Red = median, blue = mean, green = first quartile, grey = minimum"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

### Large (10 - 5000 um)s

### tier2_large_median
ggplot(pred_tier2_large_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_tier2_large_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: tier2_large_mean ###
  geom_xribbon(data = pred_tier2_large_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_tier2_large_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_tier2_large_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: tier2_large_1q  ###
    geom_xribbon(data = pred_tier2_large_1q , aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_tier2_large_1q , aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_tier2_large_1q , aes(x = Conc, y =frac), 
                 color = "green") +  
  
    ### fourth line: tier2_large_min  ###
    geom_xribbon(data = pred_tier2_large_min , aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_tier2_large_min , aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_tier2_large_min , aes(x = Conc, y =frac), 
                 color = "grey10") +  
### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (Tier 2)",
       subtitle = ("Size Range: 10 - 5,000 um; Red = median, blue = mean, green = first quartile, grey = minimum"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

3.3 Tier 3/4

3.3.1 Small (1-10 um)s

### tier3.4_small_median
ggplot(pred_tier3.4_small_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_tier3.4_small_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: tier3.4_small_mean ###
  geom_xribbon(data = pred_tier3.4_small_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_tier3.4_small_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_tier3.4_small_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: tier3.4_small_1q ###
    geom_xribbon(data = pred_tier3.4_small_1q, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_tier3.4_small_1q, aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_tier3.4_small_1q, aes(x = Conc, y =frac), 
                 color = "green") +  
  
      ### fourth line: tier3.4_small_min ###
    geom_xribbon(data = pred_tier3.4_small_min, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_tier3.4_small_min, aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_tier3.4_small_min, aes(x = Conc, y =frac), 
                 color = "grey10") + 
### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (Tier 3/4)",
       subtitle = ("Size Range: 1 - 10 um; Red = median, blue = mean, green = first quartile, grey = min"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

### Large (10 - 5000 um)s

### tier3.4_large_median
ggplot(pred_tier3.4_large_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_tier3.4_large_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: tier3.4_large_mean ###
  geom_xribbon(data = pred_tier3.4_large_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_tier3.4_large_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_tier3.4_large_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: tier3.4_large_1q ###
    geom_xribbon(data = pred_tier3.4_large_1q, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_tier3.4_large_1q, aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_tier3.4_large_1q, aes(x = Conc, y =frac), 
                 color = "green") +  
  
      ### fourth line: tier3.4_large_min  ###
    geom_xribbon(data = pred_tier3.4_large_min , aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_tier3.4_large_min , aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_tier3.4_large_min , aes(x = Conc, y =frac), 
                 color = "grey10") +  
### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (Tier 3/4)",
       subtitle = ("Size Range: 10 - 5,000 um; Red = median, blue = mean, green = first quartile, grey = minimum"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

3.4 Summary Table

# Small/Large x tier 1/2/3 x median/mean/q1 = 2 x 3 x 3 = 18 comparisons
larges <- list(tier1_large_median_hc5, tier1_large_mean_hc5, tier1_large_1q_hc5, tier1_large_min_hc5,
              tier1_large.obs[[1,1]], #n.obs
              tier1_large.obs[[1,2]], #n.studies
              tier1_large.obs[[1,3]], #n.taxa
              tier1_large.obs[[1,4]], #n.species
          #tier 2
          tier2_large_median_hc5, tier2_large_mean_hc5, tier2_large_1q_hc5, tier2_large_min_hc5,
          tier2_large.obs[[1,1]], #n.obs
          tier2_large.obs[[1,2]], #n.studies
          tier2_large.obs[[1,3]], #n.taxa
          tier2_large.obs[[1,4]], #n.species
          #tier 3 (hc5)
          tier3.4_large_median_hc5, tier3.4_large_mean_hc5, tier3.4_large_1q_hc5, tier3.4_large_min_hc5,
          tier3.4_large.obs[[1,1]], #n.obs
          tier3.4_large.obs[[1,2]], #n.studies
          tier3.4_large.obs[[1,3]], #n.taxa
          tier3.4_large.obs[[1,4]], #n.species
          #tier 4 (hc10)
          tier3.4_large_median_hc10, tier3.4_large_mean_hc10, tier3.4_large_1q_hc10, tier3.4_large_min_hc10,
          tier3.4_large.obs[[1,1]], #n.obs
          tier3.4_large.obs[[1,2]], #n.studies
          tier3.4_large.obs[[1,3]], #n.taxa
          tier3.4_large.obs[[1,4]] #n.species
          )

smalls <- list(tier1_small_median_hc5, tier1_small_mean_hc5, tier1_small_1q_hc5, tier1_small_min_hc5,
               tier1_small.obs[[1,1]], #n.obs
               tier1_small.obs[[1,2]], #n.studies
               tier1_small.obs[[1,3]], #n.taxa
               tier1_small.obs[[1,4]], #n.species
          #tier 2
          tier2_small_median_hc5, tier2_small_mean_hc5, tier2_small_1q_hc5, tier2_small_min_hc5,
          tier2_small.obs[[1,1]], #n.obs
          tier2_small.obs[[1,2]], #n.studies
          tier2_small.obs[[1,3]], #n.taxa
          tier2_small.obs[[1,4]], #n.species
          #tier 3 (hc5)
          tier3.4_small_median_hc5, tier3.4_small_mean_hc5, tier3.4_small_1q_hc5, tier3.4_small_min_hc5,
          tier3.4_small.obs[[1,1]], #n.obs
          tier3.4_small.obs[[1,2]], #n.studies
          tier3.4_small.obs[[1,3]], #n.taxa
          tier3.4_small.obs[[1,4]], #n.specie
          #tier 4 (hc10)
          tier3.4_small_median_hc10, tier3.4_small_mean_hc10, tier3.4_small_1q_hc10, tier3.4_small_min_hc10,
          tier3.4_small.obs[[1,1]], #n.obs
          tier3.4_small.obs[[1,2]], #n.studies
          tier3.4_small.obs[[1,3]], #n.taxa
          tier3.4_small.obs[[1,4]] #n.specie
          )

## Conver to data frames
largesdf <- data.frame(t(matrix(unlist(larges), ncol = 4)))
smallsdf <- data.frame(t(matrix(unlist(smalls), ncol = 4))) #t = transpose

# convert mg/L to ug/L
largesdf <- largesdf %>% 
  mutate_at(c(1:4), as.numeric) %>%
  mutate_at(c(1:4), funs(. * 1000)) %>% #don't convert n.obs and n.doi
  mutate_at(c(5:6), as.character) #make sure DOI and n are characters so they don't get color coded below

smallsdf <- smallsdf %>% 
  mutate_at(c(1:4), as.numeric) %>%
  mutate_at(c(1:4), funs(. * 1000)) %>% #don't convert n.obs and n.doi
  mutate_at(c(5:6), as.character) #make sure DOI and n are characters so they don't get color coded below

# rename columns and rows
colnames(largesdf) <- c("Median", "Mean", "Quartile_1", "Minimum", "N_Obs.", "N_Doi", "N_Taxa", "N_Species")
colnames(smallsdf) <- c("Median", "Mean", "Quartile_1", "Minimum", "N_Obs.", "N_Doi", "N_Taxa", "N_Species")

rownames(largesdf) <- c("Tier 1", "Tier 2", "Tier 3", "Tier 4")
rownames(smallsdf) <- c("Tier 1", "Tier 2", "Tier 3", "Tier 4")

# provide tier column
largestibble <- tibble(largesdf,
                       Tier = rownames(largesdf))

#re-arrange columns
largestibble <- largestibble[c("Tier", "Mean", "Median", "Quartile_1", "Minimum", "N_Obs.", "N_Doi", "N_Taxa", "N_Species")]

# provide tier column
smallstibble <- tibble(smallsdf,
                       Tier = rownames(smallsdf))

#re-arrange columns
smallstibble <- smallstibble[c("Tier", "Mean", "Median", "Quartile_1", "Minimum", "N_Obs.", "N_Doi", "N_Taxa", "N_Species")]

#function to create tables
hp_table <- function(x){
  #get full value range for whole table conditional formatting
  full_val_range <- x %>% 
    ungroup %>% 
    select_if(is.numeric) %>% 
    range
  
  #format colors accordingly
  gt(x) %>% 
    fmt_number(columns = vars(Mean, Median, Quartile_1, Minimum),
             n_sigfig = 2,
             use_seps = TRUE) %>% 
    data_color(
    columns = names(x)[2:5],
    colors = scales::col_numeric(
      palette = paletteer::paletteer_d(palette = "ggsci::teal_material") %>% as.character(),
      domain = full_val_range),
    alpha = 0.75) %>% 
    tab_options(column_labels.hidden = FALSE) %>% 
    as_raw_html() # return as html
}

#larges gt
larges_gt <- largestibble %>% 
  hp_table()

#smalls gt
smalls_gt <- smallstibble %>% 
  hp_table() 
  
## Combine
data.tables <- data.frame(smalls_table = smalls_gt,
                          larges_table = larges_gt)
#Combine
allPass.summary <- data.tables %>% 
  gt() %>% 
  fmt_markdown(columns = TRUE) %>% 
  cols_label(smalls_table = "1-10 um",
             larges_table = "10-5,000 um") %>% 
  tab_source_note(md("Calculated from SSDs")) %>% 
  tab_header(title = "Microplastics HC5 (ug/L) by Data Collapse and Tier",
             subtitle = "Qualiter Filters: Risk and Tech Tier") 

allPass.summary
Microplastics HC5 (ug/L) by Data Collapse and Tier
Qualiter Filters: Risk and Tech Tier
1-10 um 10-5,000 um
Tier Mean Median Quartile_1 Minimum N_Obs. N_Doi N_Taxa N_Species
Tier 1 410,000 28,000 630 11 212 28 7 26
Tier 2 600,000 46,000 4,400 57 163 22 7 22
Tier 3 590,000 45,000 4,300 57 161 22 7 22
Tier 4 2,100,000 220,000 32,000 860 161 22 7 22
Tier Mean Median Quartile_1 Minimum N_Obs. N_Doi N_Taxa N_Species
Tier 1 6.6 7.4 1.7 0.69 387 25 7 17
Tier 2 20 13 5.3 0.69 129 21 6 14
Tier 3 23 12 5.0 0.95 114 21 6 14
Tier 4 330 120 49 9.4 114 21 6 14
Calculated from SSDs

4 SSD Build (Risk Tier Pass, Tech Tier Fail)

4.1 risk.pass.tier 1

4.1.1 Large (10 - 5000 um)

4.1.1.1 risk.pass.tier1_large_median

risk.pass.tier1_large_median_ggplot <- ggplot(pred_risk.pass.tier1_large_median,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_risk.pass.tier1_large_median,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_risk.pass.tier1_large_median, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "risk.pass.tier1_large_median") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
risk.pass.tier1_large_median_ggplot

4.1.1.2 risk.pass.tier1_large_mean

risk.pass.tier1_large_mean_ggplot <- ggplot(pred_risk.pass.tier1_large_mean,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_risk.pass.tier1_large_mean,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_risk.pass.tier1_large_mean, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "risk.pass.tier1_large_mean") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
risk.pass.tier1_large_mean_ggplot

4.1.1.3 risk.pass.tier1_large_1q

4.1.1.4 risk.pass.tier1_large_min

4.1.2 Small (1-10 um)

4.1.2.1 risk.pass.tier1_small_median

4.1.2.2 risk.pass.tier1_small_mean

4.1.2.3 risk.pass.tier1_small_1q

4.1.2.4 risk.pass.tier1_small_min

4.2 risk.pass.tier 2

4.2.1 Large (10 - 5000 um)

4.2.1.1 risk.pass.tier2_large_median

risk.pass.tier2_large_median_ggplot <- ggplot(pred_risk.pass.tier2_large_median,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_risk.pass.tier2_large_median,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_risk.pass.tier2_large_median, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "risk.pass.tier2_large_median") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
risk.pass.tier2_large_median_ggplot

4.2.1.2 risk.pass.tier2_large_mean

risk.pass.tier2_large_mean_ggplot <- ggplot(pred_risk.pass.tier2_large_mean,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_risk.pass.tier2_large_mean,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_risk.pass.tier2_large_mean, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "risk.pass.tier2_large_mean") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
risk.pass.tier2_large_mean_ggplot

4.2.1.3 risk.pass.tier2_large_1q

4.2.1.4 risk.pass.tier2_large_min

4.2.2 Small (1-10 um)

4.2.2.1 risk.pass.tier2_small_median

4.2.2.2 risk.pass.tier2_small_mean

4.2.2.3 risk.pass.tier2_small_1q

4.2.2.4 risk.pass.tier2_small_min

4.3 risk.pass.tier 3

4.3.1 Large (10 - 5000 um)

4.3.1.1 risk.pass.tier3.4_large_median

risk.pass.tier3.4_large_median_ggplot <- ggplot(pred_risk.pass.tier3.4_large_median,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_risk.pass.tier3.4_large_median,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_risk.pass.tier3.4_large_median, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "risk.pass.tier3.4_large_median") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
risk.pass.tier3.4_large_median_ggplot

4.3.1.2 risk.pass.tier3.4_large_mean

risk.pass.tier3.4_large_mean_ggplot <- ggplot(pred_risk.pass.tier3.4_large_mean,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_risk.pass.tier3.4_large_mean,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_risk.pass.tier3.4_large_mean, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "risk.pass.tier3.4_large_mean") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
risk.pass.tier3.4_large_mean_ggplot

4.3.1.3 risk.pass.tier3.4_large_1q

4.3.1.4 risk.pass.tier3.4_large_min

4.3.2 Small (1-10 um)

4.3.2.1 risk.pass.tier3.4_small_median

4.3.2.2 risk.pass.tier3.4_small_mean

4.3.2.3 risk.pass.tier3.4_small_1q

4.3.2.4 risk.pass.tier3.4_small_min

5 Combine plots

5.1 risk.pass.tier 1

5.1.1 Small (1-10 um)s

### risk.pass.tier1_small_median
ggplot(pred_risk.pass.tier1_small_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_risk.pass.tier1_small_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: risk.pass.tier1_small_mean ###
  geom_xribbon(data = pred_risk.pass.tier1_small_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_risk.pass.tier1_small_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_risk.pass.tier1_small_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: risk.pass.tier1_small_1q ###
    geom_xribbon(data = pred_risk.pass.tier1_small_1q, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_risk.pass.tier1_small_1q, aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_risk.pass.tier1_small_1q, aes(x = Conc, y =frac), 
                 color = "green") +  
  
### fourth line: risk.pass.tier1_small_min ###
    geom_xribbon(data = pred_risk.pass.tier1_small_min, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_risk.pass.tier1_small_min, aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_risk.pass.tier1_small_min, aes(x = Conc, y =frac), 
                 color = "grey10") +  
### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (risk.pass.tier 1)",
       subtitle = ("Size Range: 1 - 10 um; Red = median, blue = mean, green = first quartile, grey = minimum"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

### Large (10 - 5000 um)s

### risk.pass.tier1_large_median
ggplot(pred_risk.pass.tier1_large_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_risk.pass.tier1_large_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: risk.pass.tier1_large_mean ###
  geom_xribbon(data = pred_risk.pass.tier1_large_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_risk.pass.tier1_large_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_risk.pass.tier1_large_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: risk.pass.tier1_large_1q ###
    geom_xribbon(data = pred_risk.pass.tier1_large_1q, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_risk.pass.tier1_large_1q, aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_risk.pass.tier1_large_1q, aes(x = Conc, y =frac), 
                 color = "green") +  
  
  ### fourth line: risk.pass.tier1_large_min ###
    geom_xribbon(data = pred_risk.pass.tier1_large_min, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_risk.pass.tier1_large_min, aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_risk.pass.tier1_large_min, aes(x = Conc, y =frac), 
                 color = "grey10") +  
### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (risk.pass.tier 1)",
       subtitle = ("Size Range: 10 - 5,000 um; Red = median, blue = mean, green = first quartile, grey = minimum"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

5.2 risk.pass.tier 2

5.2.1 Small (1-10 um)s

### risk.pass.tier2_small_median
ggplot(pred_risk.pass.tier2_small_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_risk.pass.tier2_small_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: risk.pass.tier2_small_mean ###
  geom_xribbon(data = pred_risk.pass.tier2_small_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_risk.pass.tier2_small_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_risk.pass.tier2_small_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: risk.pass.tier2_small_1q ###
    geom_xribbon(data = pred_risk.pass.tier2_small_1q, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_risk.pass.tier2_small_1q, aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_risk.pass.tier2_small_1q, aes(x = Conc, y =frac), 
                 color = "green") +  
  ### fourth line: risk.pass.tier2_small_min  ###
    geom_xribbon(data = pred_risk.pass.tier2_small_min , aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_risk.pass.tier2_small_min , aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_risk.pass.tier2_small_min , aes(x = Conc, y =frac), 
                 color = "grey10") +  
  ### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (risk.pass.tier 2)",
       subtitle = ("Size Range: 1 - 10 um; Red = median, blue = mean, green = first quartile, grey = minimum"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

### Large (10 - 5000 um)s

### risk.pass.tier2_large_median
ggplot(pred_risk.pass.tier2_large_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_risk.pass.tier2_large_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: risk.pass.tier2_large_mean ###
  geom_xribbon(data = pred_risk.pass.tier2_large_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_risk.pass.tier2_large_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_risk.pass.tier2_large_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: risk.pass.tier2_large_1q  ###
    geom_xribbon(data = pred_risk.pass.tier2_large_1q , aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_risk.pass.tier2_large_1q , aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_risk.pass.tier2_large_1q , aes(x = Conc, y =frac), 
                 color = "green") +  
  
    ### fourth line: risk.pass.tier2_large_min  ###
    geom_xribbon(data = pred_risk.pass.tier2_large_min , aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_risk.pass.tier2_large_min , aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_risk.pass.tier2_large_min , aes(x = Conc, y =frac), 
                 color = "grey10") +  
### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (risk.pass.tier 2)",
       subtitle = ("Size Range: 10 - 5,000 um; Red = median, blue = mean, green = first quartile, grey = minimum"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

5.3 risk.pass.tier 3/4

5.3.1 Small (1-10 um)s

### risk.pass.tier3.4_small_median
ggplot(pred_risk.pass.tier3.4_small_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_risk.pass.tier3.4_small_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: risk.pass.tier3.4_small_mean ###
  geom_xribbon(data = pred_risk.pass.tier3.4_small_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_risk.pass.tier3.4_small_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_risk.pass.tier3.4_small_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: risk.pass.tier3.4_small_1q ###
    geom_xribbon(data = pred_risk.pass.tier3.4_small_1q, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_risk.pass.tier3.4_small_1q, aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_risk.pass.tier3.4_small_1q, aes(x = Conc, y =frac), 
                 color = "green") +  
  
      ### fourth line: risk.pass.tier3.4_small_min ###
    geom_xribbon(data = pred_risk.pass.tier3.4_small_min, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_risk.pass.tier3.4_small_min, aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_risk.pass.tier3.4_small_min, aes(x = Conc, y =frac), 
                 color = "grey10") + 
### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (risk.pass.tier 3/4)",
       subtitle = ("Size Range: 1 - 10 um; Red = median, blue = mean, green = first quartile, grey = min"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

### Large (10 - 5000 um)s

### risk.pass.tier3.4_large_median
ggplot(pred_risk.pass.tier3.4_large_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_risk.pass.tier3.4_large_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: risk.pass.tier3.4_large_mean ###
  geom_xribbon(data = pred_risk.pass.tier3.4_large_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_risk.pass.tier3.4_large_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_risk.pass.tier3.4_large_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: risk.pass.tier3.4_large_1q ###
    geom_xribbon(data = pred_risk.pass.tier3.4_large_1q, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_risk.pass.tier3.4_large_1q, aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_risk.pass.tier3.4_large_1q, aes(x = Conc, y =frac), 
                 color = "green") +  
  
      ### fourth line: risk.pass.tier3.4_large_min  ###
    geom_xribbon(data = pred_risk.pass.tier3.4_large_min , aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_risk.pass.tier3.4_large_min , aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_risk.pass.tier3.4_large_min , aes(x = Conc, y =frac), 
                 color = "grey10") +  
### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (risk.pass.tier 3/4)",
       subtitle = ("Size Range: 10 - 5,000 um; Red = median, blue = mean, green = first quartile, grey = minimum"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

5.4 Summary Table

# Small/Large x risk.pass.tier 1/2/3 x median/mean/q1 = 2 x 3 x 3 = 18 comparisons
larges <- list(risk.pass.tier1_large_median_hc5, risk.pass.tier1_large_mean_hc5, risk.pass.tier1_large_1q_hc5, risk.pass.tier1_large_min_hc5,
              risk.pass.tier1_large.obs[[1,1]], #n.obs
              risk.pass.tier1_large.obs[[1,2]], #n.studies
              risk.pass.tier1_large.obs[[1,3]], #n.taxa
              risk.pass.tier1_large.obs[[1,4]], #n.species
          #risk.pass.tier 2
          risk.pass.tier2_large_median_hc5, risk.pass.tier2_large_mean_hc5, risk.pass.tier2_large_1q_hc5, risk.pass.tier2_large_min_hc5,
          risk.pass.tier2_large.obs[[1,1]], #n.obs
          risk.pass.tier2_large.obs[[1,2]], #n.studies
          risk.pass.tier2_large.obs[[1,3]], #n.taxa
          risk.pass.tier2_large.obs[[1,4]], #n.species
          #risk.pass.tier 3 (hc5)
          risk.pass.tier3.4_large_median_hc5, risk.pass.tier3.4_large_mean_hc5, risk.pass.tier3.4_large_1q_hc5, risk.pass.tier3.4_large_min_hc5,
          risk.pass.tier3.4_large.obs[[1,1]], #n.obs
          risk.pass.tier3.4_large.obs[[1,2]], #n.studies
          risk.pass.tier3.4_large.obs[[1,3]], #n.taxa
          risk.pass.tier3.4_large.obs[[1,4]], #n.species
          #risk.pass.tier 4 (hc10)
          risk.pass.tier3.4_large_median_hc10, risk.pass.tier3.4_large_mean_hc10, risk.pass.tier3.4_large_1q_hc10, risk.pass.tier3.4_large_min_hc10,
          risk.pass.tier3.4_large.obs[[1,1]], #n.obs
          risk.pass.tier3.4_large.obs[[1,2]], #n.studies
          risk.pass.tier3.4_large.obs[[1,3]], #n.taxa
          risk.pass.tier3.4_large.obs[[1,4]]  #n.species
          )

smalls <- list(risk.pass.tier1_small_median_hc5, risk.pass.tier1_small_mean_hc5, risk.pass.tier1_small_1q_hc5, risk.pass.tier1_small_min_hc5,
               risk.pass.tier1_small.obs[[1,1]], #n.obs
               risk.pass.tier1_small.obs[[1,2]], #n.studies
               risk.pass.tier1_small.obs[[1,3]], #n.taxa
               risk.pass.tier1_small.obs[[1,4]], #n.species
          #risk.pass.tier 2
          risk.pass.tier2_small_median_hc5, risk.pass.tier2_small_mean_hc5, risk.pass.tier2_small_1q_hc5, risk.pass.tier2_small_min_hc5,
          risk.pass.tier2_small.obs[[1,1]], #n.obs
          risk.pass.tier2_small.obs[[1,2]], #n.studies
          risk.pass.tier2_small.obs[[1,3]], #n.taxa
          risk.pass.tier2_small.obs[[1,4]], #n.species
          #risk.pass.tier 3 (hc5)
          risk.pass.tier3.4_small_median_hc5, risk.pass.tier3.4_small_mean_hc5, risk.pass.tier3.4_small_1q_hc5, risk.pass.tier3.4_small_min_hc5,
          risk.pass.tier3.4_small.obs[[1,1]], #n.obs
          risk.pass.tier3.4_small.obs[[1,2]], #n.studies
          risk.pass.tier3.4_small.obs[[1,3]], #n.taxa
          risk.pass.tier3.4_small.obs[[1,4]], #n.species
          #risk.pass.tier 4 (hc10)
          risk.pass.tier3.4_small_median_hc10, risk.pass.tier3.4_small_mean_hc10, risk.pass.tier3.4_small_1q_hc10, risk.pass.tier3.4_small_min_hc10,
          risk.pass.tier3.4_small.obs[[1,1]], #n.obs
          risk.pass.tier3.4_small.obs[[1,2]], #n.studies
          risk.pass.tier3.4_small.obs[[1,3]], #n.taxa
          risk.pass.tier3.4_small.obs[[1,4]]  #n.species
          )

## Conver to data frames
largesdf <- data.frame(t(matrix(unlist(larges), ncol = 4)))
smallsdf <- data.frame(t(matrix(unlist(smalls), ncol = 4))) #t = transpose

# convert mg/L to ug/L
largesdf <- largesdf %>% 
  mutate_at(c(1:4), as.numeric) %>%
  mutate_at(c(1:4), funs(. * 1000)) %>% #don't convert n.obs and n.doi
  mutate_at(c(5:6), as.character) #make sure DOI and n are characters so they don't get color coded below

smallsdf <- smallsdf %>% 
  mutate_at(c(1:4), as.numeric) %>%
  mutate_at(c(1:4), funs(. * 1000)) %>% #don't convert n.obs and n.doi
  mutate_at(c(5:6), as.character) #make sure DOI and n are characters so they don't get color coded below

# rename columns and rows
colnames(largesdf) <- c("Median", "Mean", "Quartile_1", "Minimum", "N_Obs.", "N_Doi", "N_Taxa", "N_Species")
colnames(smallsdf) <- c("Median", "Mean", "Quartile_1", "Minimum", "N_Obs.", "N_Doi", "N_Taxa", "N_Species")

rownames(largesdf) <- c("tier 1", "tier 2", "tier 3", "tier 4")
rownames(smallsdf) <- c("tier 1", "tier 2", "tier 3", "tier 4")

# provide risk.pass.tier column
largestibble <- tibble(largesdf,
                       tier = rownames(largesdf))

#re-arrange columns
largestibble <- largestibble[c("tier", "Mean", "Median", "Quartile_1", "Minimum", "N_Obs.", "N_Doi", "N_Taxa", "N_Species")]

# provide risk.pass.tier column
smallstibble <- tibble(smallsdf,
                       tier = rownames(smallsdf))

#re-arrange columns
smallstibble <- smallstibble[c("tier", "Mean", "Median", "Quartile_1", "Minimum", "N_Obs.", "N_Doi", "N_Taxa", "N_Species")]

#function to create tables
hp_table <- function(x){
  #get full value range for whole table conditional formatting
  full_val_range <- x %>% 
    ungroup %>% 
    select_if(is.numeric) %>% 
    range
  
  #format colors accordingly
  gt(x) %>% 
    fmt_number(columns = vars(Mean, Median, Quartile_1, Minimum),
             n_sigfig = 2,
             use_seps = TRUE) %>% 
    data_color(
    columns = names(x)[2:5],
    colors = scales::col_numeric(
      palette = paletteer::paletteer_d(palette = "ggsci::teal_material") %>% as.character(),
      domain = full_val_range),
    alpha = 0.75) %>% 
    tab_options(column_labels.hidden = FALSE) %>% 
    as_raw_html() # return as html
}

#larges gt
larges_gt <- largestibble %>% 
  hp_table()

#smalls gt
smalls_gt <- smallstibble %>% 
  hp_table() 
  
## Combine
data.tables <- data.frame(smalls_table = smalls_gt,
                          larges_table = larges_gt)
#Combine
risk.pass.summary <- data.tables %>% 
  gt() %>% 
  fmt_markdown(columns = TRUE) %>% 
  cols_label(smalls_table = "1-10 um",
             larges_table = "10-5,000 um") %>% 
  tab_source_note(md("Calculated from SSDs")) %>% 
  tab_header(title = "Microplastics HC5 (ug/L) by Data Collapse and risk.pass.tier",
             subtitle = "Qualiter Filters: Risk (pass), tech (pass & fail)") 
#print
risk.pass.summary
Microplastics HC5 (ug/L) by Data Collapse and risk.pass.tier
Qualiter Filters: Risk (pass), tech (pass & fail)
1-10 um 10-5,000 um
tier Mean Median Quartile_1 Minimum N_Obs. N_Doi N_Taxa N_Species
tier 1 430,000 48,000 470 10 264 29 7 26
tier 2 640,000 54,000 3,000 65 215 23 7 22
tier 3 620,000 53,000 2,600 65 213 23 7 22
tier 4 2,200,000 240,000 23,000 640 213 23 7 22
tier Mean Median Quartile_1 Minimum N_Obs. N_Doi N_Taxa N_Species
tier 1 7.3 9.1 1.9 0.26 433 26 7 18
tier 2 18 14 7.0 0.26 137 22 6 15
tier 3 20 14 6.6 1.4 118 22 6 15
tier 4 270 120 57 13 118 22 6 15
Calculated from SSDs

6 SSD Build (Tech Tier Pass, Risk Tier Fail)

6.1 tech.pass.tier 1

6.1.1 Large (10 - 5000 um)

6.1.1.1 tech.pass.tier1_large_median

tech.pass.tier1_large_median_ggplot <- ggplot(pred_tech.pass.tier1_large_median,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_tech.pass.tier1_large_median,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_tech.pass.tier1_large_median, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "tech.pass.tier1_large_median") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
tech.pass.tier1_large_median_ggplot

6.1.1.2 tech.pass.tier1_large_mean

tech.pass.tier1_large_mean_ggplot <- ggplot(pred_tech.pass.tier1_large_mean,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_tech.pass.tier1_large_mean,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_tech.pass.tier1_large_mean, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "tech.pass.tier1_large_mean") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
tech.pass.tier1_large_mean_ggplot

6.1.1.3 tech.pass.tier1_large_1q

6.1.1.4 tech.pass.tier1_large_min

6.1.2 Small (1-10 um)

6.1.2.1 tech.pass.tier1_small_median

6.1.2.2 tech.pass.tier1_small_mean

6.1.2.3 tech.pass.tier1_small_1q

6.1.2.4 tech.pass.tier1_small_min

6.2 tech.pass.tier 2

6.2.1 Large (10 - 5000 um)

6.2.1.1 tech.pass.tier2_large_median

tech.pass.tier2_large_median_ggplot <- ggplot(pred_tech.pass.tier2_large_median,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_tech.pass.tier2_large_median,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_tech.pass.tier2_large_median, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "tech.pass.tier2_large_median") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
tech.pass.tier2_large_median_ggplot

6.2.1.2 tech.pass.tier2_large_mean

tech.pass.tier2_large_mean_ggplot <- ggplot(pred_tech.pass.tier2_large_mean,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_tech.pass.tier2_large_mean,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_tech.pass.tier2_large_mean, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "tech.pass.tier2_large_mean") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
tech.pass.tier2_large_mean_ggplot

6.2.1.3 tech.pass.tier2_large_1q

6.2.1.4 tech.pass.tier2_large_min

6.2.2 Small (1-10 um)

6.2.2.1 tech.pass.tier2_small_median

6.2.2.2 tech.pass.tier2_small_mean

6.2.2.3 tech.pass.tier2_small_1q

6.2.2.4 tech.pass.tier2_small_min

6.3 tech.pass.tier 3

6.3.1 Large (10 - 5000 um)

6.3.1.1 tech.pass.tier3.4_large_median

tech.pass.tier3.4_large_median_ggplot <- ggplot(pred_tech.pass.tier3.4_large_median,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_tech.pass.tier3.4_large_median,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_tech.pass.tier3.4_large_median, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "tech.pass.tier3.4_large_median") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
tech.pass.tier3.4_large_median_ggplot

6.3.1.2 tech.pass.tier3.4_large_mean

tech.pass.tier3.4_large_mean_ggplot <- ggplot(pred_tech.pass.tier3.4_large_mean,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_tech.pass.tier3.4_large_mean,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_tech.pass.tier3.4_large_mean, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "tech.pass.tier3.4_large_mean") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
tech.pass.tier3.4_large_mean_ggplot

6.3.1.3 tech.pass.tier3.4_large_1q

6.3.1.4 tech.pass.tier3.4_large_min

6.3.2 Small (1-10 um)

6.3.2.1 tech.pass.tier3.4_small_median

6.3.2.2 tech.pass.tier3.4_small_mean

6.3.2.3 tech.pass.tier3.4_small_1q

6.3.2.4 tech.pass.tier3.4_small_min

7 Combine plots

7.1 tech.pass.tier 1

7.1.1 Small (1-10 um)s

### tech.pass.tier1_small_median
ggplot(pred_tech.pass.tier1_small_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_tech.pass.tier1_small_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: tech.pass.tier1_small_mean ###
  geom_xribbon(data = pred_tech.pass.tier1_small_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_tech.pass.tier1_small_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_tech.pass.tier1_small_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: tech.pass.tier1_small_1q ###
    geom_xribbon(data = pred_tech.pass.tier1_small_1q, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_tech.pass.tier1_small_1q, aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_tech.pass.tier1_small_1q, aes(x = Conc, y =frac), 
                 color = "green") +  
  
### fourth line: tech.pass.tier1_small_min ###
    geom_xribbon(data = pred_tech.pass.tier1_small_min, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_tech.pass.tier1_small_min, aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_tech.pass.tier1_small_min, aes(x = Conc, y =frac), 
                 color = "grey10") +  
### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (tech.pass.tier 1)",
       subtitle = ("Size Range: 1 - 10 um; Red = median, blue = mean, green = first quartile, grey = minimum"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

### Large (10 - 5000 um)s

### tech.pass.tier1_large_median
ggplot(pred_tech.pass.tier1_large_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_tech.pass.tier1_large_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: tech.pass.tier1_large_mean ###
  geom_xribbon(data = pred_tech.pass.tier1_large_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_tech.pass.tier1_large_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_tech.pass.tier1_large_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: tech.pass.tier1_large_1q ###
    geom_xribbon(data = pred_tech.pass.tier1_large_1q, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_tech.pass.tier1_large_1q, aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_tech.pass.tier1_large_1q, aes(x = Conc, y =frac), 
                 color = "green") +  
  
  ### fourth line: tech.pass.tier1_large_min ###
    geom_xribbon(data = pred_tech.pass.tier1_large_min, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_tech.pass.tier1_large_min, aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_tech.pass.tier1_large_min, aes(x = Conc, y =frac), 
                 color = "grey10") +  
### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (tech.pass.tier 1)",
       subtitle = ("Size Range: 10 - 5,000 um; Red = median, blue = mean, green = first quartile, grey = minimum"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

7.2 tech.pass.tier 2

7.2.1 Small (1-10 um)s

### tech.pass.tier2_small_median
ggplot(pred_tech.pass.tier2_small_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_tech.pass.tier2_small_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: tech.pass.tier2_small_mean ###
  geom_xribbon(data = pred_tech.pass.tier2_small_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_tech.pass.tier2_small_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_tech.pass.tier2_small_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: tech.pass.tier2_small_1q ###
    geom_xribbon(data = pred_tech.pass.tier2_small_1q, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_tech.pass.tier2_small_1q, aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_tech.pass.tier2_small_1q, aes(x = Conc, y =frac), 
                 color = "green") +  
  ### fourth line: tech.pass.tier2_small_min  ###
    geom_xribbon(data = pred_tech.pass.tier2_small_min , aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_tech.pass.tier2_small_min , aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_tech.pass.tier2_small_min , aes(x = Conc, y =frac), 
                 color = "grey10") +  
  ### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (tech.pass.tier 2)",
       subtitle = ("Size Range: 1 - 10 um; Red = median, blue = mean, green = first quartile, grey = minimum"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

### Large (10 - 5000 um)s

### tech.pass.tier2_large_median
ggplot(pred_tech.pass.tier2_large_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_tech.pass.tier2_large_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: tech.pass.tier2_large_mean ###
  geom_xribbon(data = pred_tech.pass.tier2_large_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_tech.pass.tier2_large_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_tech.pass.tier2_large_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: tech.pass.tier2_large_1q  ###
    geom_xribbon(data = pred_tech.pass.tier2_large_1q , aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_tech.pass.tier2_large_1q , aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_tech.pass.tier2_large_1q , aes(x = Conc, y =frac), 
                 color = "green") +  
  
    ### fourth line: tech.pass.tier2_large_min  ###
    geom_xribbon(data = pred_tech.pass.tier2_large_min , aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_tech.pass.tier2_large_min , aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_tech.pass.tier2_large_min , aes(x = Conc, y =frac), 
                 color = "grey10") +  
### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (tech.pass.tier 2)",
       subtitle = ("Size Range: 10 - 5,000 um; Red = median, blue = mean, green = first quartile, grey = minimum"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

7.3 tech.pass.tier 3/4

7.3.1 Small (1-10 um)s

### tech.pass.tier3.4_small_median
ggplot(pred_tech.pass.tier3.4_small_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_tech.pass.tier3.4_small_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: tech.pass.tier3.4_small_mean ###
  geom_xribbon(data = pred_tech.pass.tier3.4_small_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_tech.pass.tier3.4_small_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_tech.pass.tier3.4_small_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: tech.pass.tier3.4_small_1q ###
    geom_xribbon(data = pred_tech.pass.tier3.4_small_1q, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_tech.pass.tier3.4_small_1q, aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_tech.pass.tier3.4_small_1q, aes(x = Conc, y =frac), 
                 color = "green") +  
  
      ### fourth line: tech.pass.tier3.4_small_min ###
    geom_xribbon(data = pred_tech.pass.tier3.4_small_min, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_tech.pass.tier3.4_small_min, aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_tech.pass.tier3.4_small_min, aes(x = Conc, y =frac), 
                 color = "grey10") + 
### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (tech.pass.tier 3/4)",
       subtitle = ("Size Range: 1 - 10 um; Red = median, blue = mean, green = first quartile, grey = min"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

### Large (10 - 5000 um)s

### tech.pass.tier3.4_large_median
ggplot(pred_tech.pass.tier3.4_large_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_tech.pass.tier3.4_large_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: tech.pass.tier3.4_large_mean ###
  geom_xribbon(data = pred_tech.pass.tier3.4_large_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_tech.pass.tier3.4_large_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_tech.pass.tier3.4_large_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: tech.pass.tier3.4_large_1q ###
    geom_xribbon(data = pred_tech.pass.tier3.4_large_1q, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_tech.pass.tier3.4_large_1q, aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_tech.pass.tier3.4_large_1q, aes(x = Conc, y =frac), 
                 color = "green") +  
  
      ### fourth line: tech.pass.tier3.4_large_min  ###
    geom_xribbon(data = pred_tech.pass.tier3.4_large_min , aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_tech.pass.tier3.4_large_min , aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_tech.pass.tier3.4_large_min , aes(x = Conc, y =frac), 
                 color = "grey10") +  
### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (tech.pass.tier 3/4)",
       subtitle = ("Size Range: 10 - 5,000 um; Red = median, blue = mean, green = first quartile, grey = minimum"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

## Summary Table

# Small/Large x tech.pass.tier 1/2/3 x median/mean/q1 = 2 x 3 x 3 = 18 comparisons
larges <- list(tech.pass.tier1_large_median_hc5, tech.pass.tier1_large_mean_hc5, tech.pass.tier1_large_1q_hc5, tech.pass.tier1_large_min_hc5,
              tech.pass.tier1_large.obs[[1,1]], #n.obs
              tech.pass.tier1_large.obs[[1,2]], #n.studies
              tech.pass.tier1_large.obs[[1,3]], #n.taxa
              tech.pass.tier1_large.obs[[1,4]], #n.species
          #tech.pass.tier 2
          tech.pass.tier2_large_median_hc5, tech.pass.tier2_large_mean_hc5, tech.pass.tier2_large_1q_hc5, tech.pass.tier2_large_min_hc5,
          tech.pass.tier2_large.obs[[1,1]], #n.obs
          tech.pass.tier2_large.obs[[1,2]], #n.studies
          tech.pass.tier2_large.obs[[1,3]], #n.taxa
          tech.pass.tier2_large.obs[[1,4]], #n.species
          #tech.pass.tier 3 (hc5)
          tech.pass.tier3.4_large_median_hc5, tech.pass.tier3.4_large_mean_hc5, tech.pass.tier3.4_large_1q_hc5, tech.pass.tier3.4_large_min_hc5,
          tech.pass.tier3.4_large.obs[[1,1]], #n.obs
          tech.pass.tier3.4_large.obs[[1,2]], #n.studies
          tech.pass.tier3.4_large.obs[[1,3]], #n.taxa
          tech.pass.tier3.4_large.obs[[1,4]], #n.species
          #tech.pass.tier 4 (hc10)
          tech.pass.tier3.4_large_median_hc10, tech.pass.tier3.4_large_mean_hc10, tech.pass.tier3.4_large_1q_hc10, tech.pass.tier3.4_large_min_hc10,
          tech.pass.tier3.4_large.obs[[1,1]], #n.obs
          tech.pass.tier3.4_large.obs[[1,2]], #n.studies
          tech.pass.tier3.4_large.obs[[1,3]], #n.taxa
          tech.pass.tier3.4_large.obs[[1,4]]  #n.species
          )

smalls <- list(tech.pass.tier1_small_median_hc5, tech.pass.tier1_small_mean_hc5, tech.pass.tier1_small_1q_hc5, tech.pass.tier1_small_min_hc5,
               tech.pass.tier1_small.obs[[1,1]], #n.obs
               tech.pass.tier1_small.obs[[1,2]], #n.studies
               tech.pass.tier1_small.obs[[1,3]], #n.taxa
               tech.pass.tier1_small.obs[[1,4]], #n.species
          #tech.pass.tier 2
          tech.pass.tier2_small_median_hc5, tech.pass.tier2_small_mean_hc5, tech.pass.tier2_small_1q_hc5, tech.pass.tier2_small_min_hc5,
          tech.pass.tier2_small.obs[[1,1]], #n.obs
          tech.pass.tier2_small.obs[[1,2]], #n.studies
          tech.pass.tier2_small.obs[[1,3]], #n.taxa
          tech.pass.tier2_small.obs[[1,4]], #n.species
          #tech.pass.tier 3 (hc5)
          tech.pass.tier3.4_small_median_hc5, tech.pass.tier3.4_small_mean_hc5, tech.pass.tier3.4_small_1q_hc5, tech.pass.tier3.4_small_min_hc5,
          tech.pass.tier3.4_small.obs[[1,1]], #n.obs
          tech.pass.tier3.4_small.obs[[1,2]], #n.studies
          tech.pass.tier3.4_small.obs[[1,3]], #n.taxa
          tech.pass.tier3.4_small.obs[[1,4]], #n.species
          #tech.pass.tier 4 (hc10)
          tech.pass.tier3.4_small_median_hc10, tech.pass.tier3.4_small_mean_hc10, tech.pass.tier3.4_small_1q_hc10, tech.pass.tier3.4_small_min_hc10,
          tech.pass.tier3.4_small.obs[[1,1]], #n.obs
          tech.pass.tier3.4_small.obs[[1,2]], #n.studies
          tech.pass.tier3.4_small.obs[[1,3]], #n.taxa
          tech.pass.tier3.4_small.obs[[1,4]]  #n.species
          )

## Conver to data frames
largesdf <- data.frame(t(matrix(unlist(larges), ncol = 4)))
smallsdf <- data.frame(t(matrix(unlist(smalls), ncol = 4))) #t = transpose

# convert mg/L to ug/L
largesdf <- largesdf %>% 
  mutate_at(c(1:4), as.numeric) %>%
  mutate_at(c(1:4), funs(. * 1000)) %>% #don't convert n.obs and n.doi
  mutate_at(c(5:6), as.character) #make sure DOI and n are characters so they don't get color coded below

smallsdf <- smallsdf %>% 
  mutate_at(c(1:4), as.numeric) %>%
  mutate_at(c(1:4), funs(. * 1000)) %>% #don't convert n.obs and n.doi
  mutate_at(c(5:6), as.character) #make sure DOI and n are characters so they don't get color coded below

# rename columns and rows
colnames(largesdf) <- c("Median", "Mean", "Quartile_1", "Minimum", "N_Obs.", "N_Doi", "N_Taxa", "N_Species")
colnames(smallsdf) <- c("Median", "Mean", "Quartile_1", "Minimum", "N_Obs.", "N_Doi", "N_Taxa", "N_Species")

rownames(largesdf) <- c("Tier 1", "Tier 2", "Tier 3", "Tier 4")
rownames(smallsdf) <- c("Tier 1", "Tier 2", "Tier 3", "Tier 4")

# provide tier column
largestibble <- tibble(largesdf,
                       Tier = rownames(largesdf))

#re-arrange columns
largestibble <- largestibble[c("Tier", "Mean", "Median", "Quartile_1", "Minimum", "N_Obs.", "N_Doi", "N_Taxa", "N_Species")]

# provide tier column
smallstibble <- tibble(smallsdf,
                       Tier = rownames(smallsdf))

#re-arrange columns
smallstibble <- smallstibble[c("Tier", "Mean", "Median", "Quartile_1", "Minimum", "N_Obs.", "N_Doi", "N_Taxa", "N_Species")]

#function to create tables
hp_table <- function(x){
  #get full value range for whole table conditional formatting
  full_val_range <- x %>% 
    ungroup %>% 
    select_if(is.numeric) %>% 
    range
  
  #format colors accordingly
  gt(x) %>% 
    fmt_number(columns = vars(Mean, Median, Quartile_1, Minimum),
             n_sigfig = 2,
             use_seps = TRUE) %>% 
    data_color(
    columns = names(x)[2:5],
    colors = scales::col_numeric(
      palette = paletteer::paletteer_d(palette = "ggsci::teal_material") %>% as.character(),
      domain = full_val_range),
    alpha = 0.75) %>% 
    tab_options(column_labels.hidden = FALSE) %>% 
    as_raw_html() # return as html
}

#larges gt
larges_gt <- largestibble %>% 
  hp_table()

#smalls gt
smalls_gt <- smallstibble %>% 
  hp_table() 
  
## Combine
data.tables <- data.frame(smalls_table = smalls_gt,
                          larges_table = larges_gt)
#Combine
tech.pass.summary <- data.tables %>% 
  gt() %>% 
  fmt_markdown(columns = TRUE) %>% 
  cols_label(smalls_table = "1-10 um",
             larges_table = "10-5,000 um") %>% 
  tab_source_note(md("Calculated from SSDs")) %>% 
  tab_header(title = "Microplastics HC5 (ug/L) by Data Collapse and Tier",
             subtitle = "Qualiter Filters: Risk (pass & fail) Tech (pass)") 
#print
tech.pass.summary
Microplastics HC5 (ug/L) by Data Collapse and Tier
Qualiter Filters: Risk (pass & fail) Tech (pass)
1-10 um 10-5,000 um
Tier Mean Median Quartile_1 Minimum N_Obs. N_Doi N_Taxa N_Species
Tier 1 77,000 9,200 870 18 467 45 7 35
Tier 2 220,000 29,000 3,500 50 240 37 7 29
Tier 3 410,000 55,000 7,600 200 199 31 7 28
Tier 4 1,600,000 280,000 57,000 3,000 199 31 7 28
Tier Mean Median Quartile_1 Minimum N_Obs. N_Doi N_Taxa N_Species
Tier 1 4.9 5.2 3.5 1.5 964 57 8 43
Tier 2 3.0 3.5 2.5 1.5 371 50 8 38
Tier 3 2.6 3.1 1.9 0.90 222 44 8 34
Tier 4 19 20 12 5.5 222 44 8 34
Calculated from SSDs

8 SSD Build(All Fail)

8.1 allFail.tier 1

8.1.1 Large (10 - 5000 um)

8.1.1.1 allFail.tier1_large_median

allFail.tier1_large_median_ggplot <- ggplot(pred_allFail.tier1_large_median,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_allFail.tier1_large_median,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_allFail.tier1_large_median, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "allFail.tier1_large_median") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
allFail.tier1_large_median_ggplot

8.1.1.2 allFail.tier1_large_mean

allFail.tier1_large_mean_ggplot <- ggplot(pred_allFail.tier1_large_mean,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_allFail.tier1_large_mean,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_allFail.tier1_large_mean, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "allFail.tier1_large_mean") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
allFail.tier1_large_mean_ggplot

8.1.1.3 allFail.tier1_large_1q

8.1.1.4 allFail.tier1_large_min

8.1.2 Small (1-10 um)

8.1.2.1 allFail.tier1_small_median

8.1.2.2 allFail.tier1_small_mean

8.1.2.3 allFail.tier1_small_1q

8.1.2.4 allFail.tier1_small_min

8.2 allFail.tier 2

8.2.1 Large (10 - 5000 um)

8.2.1.1 allFail.tier2_large_median

allFail.tier2_large_median_ggplot <- ggplot(pred_allFail.tier2_large_median,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_allFail.tier2_large_median,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_allFail.tier2_large_median, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "allFail.tier2_large_median") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
allFail.tier2_large_median_ggplot

8.2.1.2 allFail.tier2_large_mean

allFail.tier2_large_mean_ggplot <- ggplot(pred_allFail.tier2_large_mean,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_allFail.tier2_large_mean,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_allFail.tier2_large_mean, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "allFail.tier2_large_mean") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
allFail.tier2_large_mean_ggplot

8.2.1.3 allFail.tier2_large_1q

8.2.1.4 allFail.tier2_large_min

8.2.2 Small (1-10 um)

8.2.2.1 allFail.tier2_small_median

8.2.2.2 allFail.tier2_small_mean

8.2.2.3 allFail.tier2_small_1q

8.2.2.4 allFail.tier2_small_min

8.3 allFail.tier 3

8.3.1 Large (10 - 5000 um)

8.3.1.1 allFail.tier3.4_large_median

allFail.tier3.4_large_median_ggplot <- ggplot(pred_allFail.tier3.4_large_median,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_allFail.tier3.4_large_median,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_allFail.tier3.4_large_median, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "allFail.tier3.4_large_median") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
allFail.tier3.4_large_median_ggplot

8.3.1.2 allFail.tier3.4_large_mean

allFail.tier3.4_large_mean_ggplot <- ggplot(pred_allFail.tier3.4_large_mean,aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, color = "blue", fill = "lightblue") +
      geom_line(aes_string(y = "percent/100"), color = "gray") +
      geom_point(data = SSD_allFail.tier3.4_large_mean,aes(x = Conc, y =frac, color = Group)) + 
      geom_text_repel(data = SSD_allFail.tier3.4_large_mean, aes(x = Conc, y = frac, label = Species, color = Group), nudge_x = 0.2, size = 3, segment.alpha = 0.5) + 
      scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) + 
  labs(title = "allFail.tier3.4_large_mean") +
      fill.type + #user-selected
      color.type + #user-selected
      theme.type #user theme
allFail.tier3.4_large_mean_ggplot

8.3.1.3 allFail.tier3.4_large_1q

8.3.1.4 allFail.tier3.4_large_min

8.3.2 Small (1-10 um)

8.3.2.1 allFail.tier3.4_small_median

8.3.2.2 allFail.tier3.4_small_mean

8.3.2.3 allFail.tier3.4_small_1q

8.3.2.4 allFail.tier3.4_small_min

9 Combine plots

9.1 allFail.tier 1

9.1.1 Small (1-10 um)s

### allFail.tier1_small_median
ggplot(pred_allFail.tier1_small_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_allFail.tier1_small_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: allFail.tier1_small_mean ###
  geom_xribbon(data = pred_allFail.tier1_small_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_allFail.tier1_small_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_allFail.tier1_small_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: allFail.tier1_small_1q ###
    geom_xribbon(data = pred_allFail.tier1_small_1q, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_allFail.tier1_small_1q, aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_allFail.tier1_small_1q, aes(x = Conc, y =frac), 
                 color = "green") +  
  
### fourth line: allFail.tier1_small_min ###
    geom_xribbon(data = pred_allFail.tier1_small_min, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_allFail.tier1_small_min, aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_allFail.tier1_small_min, aes(x = Conc, y =frac), 
                 color = "grey10") +  
### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (allFail.tier 1)",
       subtitle = ("Size Range: 1 - 10 um; Red = median, blue = mean, green = first quartile, grey = minimum"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

### Large (10 - 5000 um)s

### allFail.tier1_large_median
ggplot(pred_allFail.tier1_large_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_allFail.tier1_large_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: allFail.tier1_large_mean ###
  geom_xribbon(data = pred_allFail.tier1_large_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_allFail.tier1_large_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_allFail.tier1_large_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: allFail.tier1_large_1q ###
    geom_xribbon(data = pred_allFail.tier1_large_1q, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_allFail.tier1_large_1q, aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_allFail.tier1_large_1q, aes(x = Conc, y =frac), 
                 color = "green") +  
  
  ### fourth line: allFail.tier1_large_min ###
    geom_xribbon(data = pred_allFail.tier1_large_min, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_allFail.tier1_large_min, aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_allFail.tier1_large_min, aes(x = Conc, y =frac), 
                 color = "grey10") +  
### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (allFail.tier 1)",
       subtitle = ("Size Range: 10 - 5,000 um; Red = median, blue = mean, green = first quartile, grey = minimum"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

9.2 allFail.tier 2

9.2.1 Small (1-10 um)s

### allFail.tier2_small_median
ggplot(pred_allFail.tier2_small_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_allFail.tier2_small_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: allFail.tier2_small_mean ###
  geom_xribbon(data = pred_allFail.tier2_small_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_allFail.tier2_small_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_allFail.tier2_small_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: allFail.tier2_small_1q ###
    geom_xribbon(data = pred_allFail.tier2_small_1q, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_allFail.tier2_small_1q, aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_allFail.tier2_small_1q, aes(x = Conc, y =frac), 
                 color = "green") +  
  ### fourth line: allFail.tier2_small_min  ###
    geom_xribbon(data = pred_allFail.tier2_small_min , aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_allFail.tier2_small_min , aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_allFail.tier2_small_min , aes(x = Conc, y =frac), 
                 color = "grey10") +  
  ### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (allFail.tier 2)",
       subtitle = ("Size Range: 1 - 10 um; Red = median, blue = mean, green = first quartile, grey = minimum"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

### Large (10 - 5000 um)s

### allFail.tier2_large_median
ggplot(pred_allFail.tier2_large_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_allFail.tier2_large_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: allFail.tier2_large_mean ###
  geom_xribbon(data = pred_allFail.tier2_large_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_allFail.tier2_large_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_allFail.tier2_large_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: allFail.tier2_large_1q  ###
    geom_xribbon(data = pred_allFail.tier2_large_1q , aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_allFail.tier2_large_1q , aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_allFail.tier2_large_1q , aes(x = Conc, y =frac), 
                 color = "green") +  
  
    ### fourth line: allFail.tier2_large_min  ###
    geom_xribbon(data = pred_allFail.tier2_large_min , aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_allFail.tier2_large_min , aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_allFail.tier2_large_min , aes(x = Conc, y =frac), 
                 color = "grey10") +  
### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (allFail.tier 2)",
       subtitle = ("Size Range: 10 - 5,000 um; Red = median, blue = mean, green = first quartile, grey = minimum"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

9.3 allFail.tier 3/4

9.3.1 Small (1-10 um)s

### allFail.tier3.4_small_median
ggplot(pred_allFail.tier3.4_small_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_allFail.tier3.4_small_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: allFail.tier3.4_small_mean ###
  geom_xribbon(data = pred_allFail.tier3.4_small_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_allFail.tier3.4_small_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_allFail.tier3.4_small_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: allFail.tier3.4_small_1q ###
    geom_xribbon(data = pred_allFail.tier3.4_small_1q, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_allFail.tier3.4_small_1q, aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_allFail.tier3.4_small_1q, aes(x = Conc, y =frac), 
                 color = "green") +  
  
      ### fourth line: allFail.tier3.4_small_min ###
    geom_xribbon(data = pred_allFail.tier3.4_small_min, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_allFail.tier3.4_small_min, aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_allFail.tier3.4_small_min, aes(x = Conc, y =frac), 
                 color = "grey10") + 
### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (allFail.tier 3/4)",
       subtitle = ("Size Range: 1 - 10 um; Red = median, blue = mean, green = first quartile, grey = min"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

### Large (10 - 5000 um)s

### allFail.tier3.4_large_median
ggplot(pred_allFail.tier3.4_large_median, aes_string(x = "est")) +
      geom_xribbon(aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
                   color = "red", fill = "red") +
      geom_line(aes_string(y = "percent/100"), 
                color = "red") +
      geom_point(data = SSD_allFail.tier3.4_large_median,aes(x = Conc, y =frac), 
                 color = "red") + 
      
### second line: allFail.tier3.4_large_mean ###
  geom_xribbon(data = pred_allFail.tier3.4_large_mean, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "blue", fill = "blue") +
      geom_line(data = pred_allFail.tier3.4_large_mean, aes_string(y = "percent/100"), 
                color = "blue") +
      geom_point(data = SSD_allFail.tier3.4_large_mean, aes(x = Conc, y =frac), 
                 color = "blue") +  
### third line: allFail.tier3.4_large_1q ###
    geom_xribbon(data = pred_allFail.tier3.4_large_1q, aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "green", fill = "green") +
      geom_line(data = pred_allFail.tier3.4_large_1q, aes_string(y = "percent/100"), 
                color = "green") +
      geom_point(data = SSD_allFail.tier3.4_large_1q, aes(x = Conc, y =frac), 
                 color = "green") +  
  
      ### fourth line: allFail.tier3.4_large_min  ###
    geom_xribbon(data = pred_allFail.tier3.4_large_min , aes_string(xmin = "lcl", xmax = "ucl", y = "percent/100"), alpha = 0.2, 
               color = "grey10", fill = "grey10") +
      geom_line(data = pred_allFail.tier3.4_large_min , aes_string(y = "percent/100"), 
                color = "grey10") +
      geom_point(data = SSD_allFail.tier3.4_large_min , aes(x = Conc, y =frac), 
                 color = "grey10") +  
### plot parameters ###
  scale_y_continuous("Species Affected (%)", labels = scales::percent, limits = c(0,1)) +
      xlab(particle_mass_check_ssd)+
      coord_trans(x = "log10") +
      scale_x_continuous(breaks = scales::trans_breaks("log10", function(x) 10^x, n = 15),
                         labels = trans_format("log10", scales::math_format(10^.x))) +
  labs(title = "Species Sensitivity Distributions by Data Collapsing (allFail.tier 3/4)",
       subtitle = ("Size Range: 10 - 5,000 um; Red = median, blue = mean, green = first quartile, grey = minimum"),
       caption = ("All endpoints")) +
  theme.type +
  theme(legend.position = "none",
    plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_text(size = 16),
        axis.text =  element_text(size = 16),
        legend.text = element_text(size =14),
        legend.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5, size = 14))

## Summary Table

# Small/Large x allFail.tier 1/2/3 x median/mean/q1 = 2 x 3 x 3 = 18 comparisons
larges <- list(allFail.tier1_large_median_hc5, allFail.tier1_large_mean_hc5, allFail.tier1_large_1q_hc5, allFail.tier1_large_min_hc5,
              allFail.tier1_large.obs[[1,1]], #n.obs
              allFail.tier1_large.obs[[1,2]], #n.studies
              allFail.tier1_large.obs[[1,3]], #n.taxa
              allFail.tier1_large.obs[[1,4]], #n.species
          #allFail.tier 2
          allFail.tier2_large_median_hc5, allFail.tier2_large_mean_hc5, allFail.tier2_large_1q_hc5, allFail.tier2_large_min_hc5,
          allFail.tier2_large.obs[[1,1]], #n.obs
          allFail.tier2_large.obs[[1,2]], #n.studies
          allFail.tier2_large.obs[[1,3]], #n.taxa
          allFail.tier2_large.obs[[1,4]], #n.species
          #allFail.tier 3 (hc5)
          allFail.tier3.4_large_median_hc5, allFail.tier3.4_large_mean_hc5, allFail.tier3.4_large_1q_hc5, allFail.tier3.4_large_min_hc5,
          allFail.tier3.4_large.obs[[1,1]], #n.obs
          allFail.tier3.4_large.obs[[1,2]], #n.studies
          allFail.tier3.4_large.obs[[1,3]], #n.taxa
          allFail.tier3.4_large.obs[[1,4]], #n.species
          #allFail.tier 4 (hc10)
          allFail.tier3.4_large_median_hc10, allFail.tier3.4_large_mean_hc10, allFail.tier3.4_large_1q_hc10, allFail.tier3.4_large_min_hc10,
          allFail.tier3.4_large.obs[[1,1]], #n.obs
          allFail.tier3.4_large.obs[[1,2]], #n.studies
          allFail.tier3.4_large.obs[[1,3]], #n.taxa
          allFail.tier3.4_large.obs[[1,4]]  #n.species
          )

smalls <- list(allFail.tier1_small_median_hc5, allFail.tier1_small_mean_hc5, allFail.tier1_small_1q_hc5, allFail.tier1_small_min_hc5,
               allFail.tier1_small.obs[[1,1]], #n.obs
               allFail.tier1_small.obs[[1,2]], #n.studies
               allFail.tier1_small.obs[[1,3]], #n.taxa
               allFail.tier1_small.obs[[1,4]], #n.species
          #allFail.tier 2
          allFail.tier2_small_median_hc5, allFail.tier2_small_mean_hc5, allFail.tier2_small_1q_hc5, allFail.tier2_small_min_hc5,
          allFail.tier2_small.obs[[1,1]], #n.obs
          allFail.tier2_small.obs[[1,2]], #n.studies
          allFail.tier2_small.obs[[1,3]], #n.taxa
          allFail.tier2_small.obs[[1,4]], #n.species
          #allFail.tier 3 (hc5)
          allFail.tier3.4_small_median_hc5, allFail.tier3.4_small_mean_hc5, allFail.tier3.4_small_1q_hc5, allFail.tier3.4_small_min_hc5,
          allFail.tier3.4_small.obs[[1,1]], #n.obs
          allFail.tier3.4_small.obs[[1,2]], #n.studies
          allFail.tier3.4_small.obs[[1,3]], #n.taxa
          allFail.tier3.4_small.obs[[1,4]], #n.species
          #allFail.tier 4 (hc10)
          allFail.tier3.4_small_median_hc10, allFail.tier3.4_small_mean_hc10, allFail.tier3.4_small_1q_hc10, allFail.tier3.4_small_min_hc10,
          allFail.tier3.4_small.obs[[1,1]], #n.obs
          allFail.tier3.4_small.obs[[1,2]], #n.studies
          allFail.tier3.4_small.obs[[1,3]], #n.taxa
          allFail.tier3.4_small.obs[[1,4]]  #n.species
          )

## Conver to data frames
largesdf <- data.frame(t(matrix(unlist(larges), ncol = 4)))
smallsdf <- data.frame(t(matrix(unlist(smalls), ncol = 4))) #t = transpose

# convert mg/L to ug/L
largesdf <- largesdf %>% 
  mutate_at(c(1:4), as.numeric) %>%
  mutate_at(c(1:4), funs(. * 1000)) %>% #don't convert n.obs and n.doi
  mutate_at(c(5:6), as.character) #make sure DOI and n are characters so they don't get color coded below

smallsdf <- smallsdf %>% 
  mutate_at(c(1:4), as.numeric) %>%
  mutate_at(c(1:4), funs(. * 1000)) %>% #don't convert n.obs and n.doi
  mutate_at(c(5:6), as.character) #make sure DOI and n are characters so they don't get color coded below

# rename columns and rows
colnames(largesdf) <- c("Median", "Mean", "Quartile_1", "Minimum", "N_Obs.", "N_Doi", "N_Taxa", "N_Species")
colnames(smallsdf) <- c("Median", "Mean", "Quartile_1", "Minimum", "N_Obs.", "N_Doi", "N_Taxa", "N_Species")

rownames(largesdf) <- c("Tier 1", "Tier 2", "Tier 3", "Tier 4")
rownames(smallsdf) <- c("Tier 1", "Tier 2", "Tier 3", "Tier 4")

# provide tier column
largestibble <- tibble(largesdf,
                       Tier = rownames(largesdf))

#re-arrange columns
largestibble <- largestibble[c("Tier", "Mean", "Median", "Quartile_1", "Minimum", "N_Obs.", "N_Doi", "N_Taxa", "N_Species")]

# provide tier column
smallstibble <- tibble(smallsdf,
                       Tier = rownames(smallsdf))

#re-arrange columns
smallstibble <- smallstibble[c("Tier", "Mean", "Median", "Quartile_1", "Minimum", "N_Obs.", "N_Doi", "N_Taxa", "N_Species")]

#function to create tables
hp_table <- function(x){
  #get full value range for whole table conditional formatting
  full_val_range <- x %>% 
    ungroup %>% 
    select_if(is.numeric) %>% 
    range
  
  #format colors accordingly
  gt(x) %>% 
    fmt_number(columns = vars(Mean, Median, Quartile_1, Minimum),
             n_sigfig = 2,
             use_seps = TRUE) %>% 
    data_color(
    columns = names(x)[2:5],
    colors = scales::col_numeric(
      palette = paletteer::paletteer_d(palette = "ggsci::teal_material") %>% as.character(),
      domain = full_val_range),
    alpha = 0.75) %>% 
    tab_options(column_labels.hidden = FALSE) %>% 
    as_raw_html() # return as html
}

#larges gt
larges_gt <- largestibble %>% 
  hp_table()

#smalls gt
smalls_gt <- smallstibble %>% 
  hp_table() 
  
## Combine
data.tables <- data.frame(smalls_table = smalls_gt,
                          larges_table = larges_gt)
#Combine
allFail.summary <- data.tables %>% 
  gt() %>% 
  fmt_markdown(columns = TRUE) %>% 
  cols_label(smalls_table = "1-10 um",
             larges_table = "10-5,000 um") %>% 
  tab_source_note(md("Calculated from SSDs")) %>% 
  tab_header(title = "Microplastics HC5 (ug/L) by Data Collapse and Tier",
             subtitle = "Qualiter Filters: Risk (pass & fail); Tech (pass & fail)") 
#print
allFail.summary
Microplastics HC5 (ug/L) by Data Collapse and Tier
Qualiter Filters: Risk (pass & fail); Tech (pass & fail)
1-10 um 10-5,000 um
Tier Mean Median Quartile_1 Minimum N_Obs. N_Doi N_Taxa N_Species
Tier 1 34,000 9,200 540 30 534 48 8 37
Tier 2 74,000 14,000 1,400 50 295 40 8 31
Tier 3 120,000 21,000 2,400 150 280 35 8 30
Tier 4 610,000 120,000 20,000 1,700 280 35 8 30
Tier Mean Median Quartile_1 Minimum N_Obs. N_Doi N_Taxa N_Species
Tier 1 3.3 3.8 2.5 0.57 1028 62 8 46
Tier 2 2.6 3.4 2.3 0.57 397 55 8 42
Tier 3 2.5 3.1 1.8 0.68 308 51 8 40
Tier 4 16 15 9.8 4.0 308 51 8 40
Calculated from SSDs

10 Grand Summary Table

## Combine data tables from all quality tiers
data.tables <- data.frame(allPass = allPass.summary,
                          risk.pass = risk.pass.summary,
                          tech.pass = tech.pass.summary,
                          allFail = allFail.summary)
#Combine
data.tables %>% 
  gt() %>% 
  fmt_markdown(columns = TRUE) #%>% 
allPass.smalls_table allPass.larges_table risk.pass.smalls_table risk.pass.larges_table tech.pass.smalls_table tech.pass.larges_table allFail.smalls_table allFail.larges_table
Tier Mean Median Quartile_1 Minimum N_Obs. N_Doi N_Taxa N_Species
Tier 1 410,000 28,000 630 11 212 28 7 26
Tier 2 600,000 46,000 4,400 57 163 22 7 22
Tier 3 590,000 45,000 4,300 57 161 22 7 22
Tier 4 2,100,000 220,000 32,000 860 161 22 7 22
Tier Mean Median Quartile_1 Minimum N_Obs. N_Doi N_Taxa N_Species
Tier 1 6.6 7.4 1.7 0.69 387 25 7 17
Tier 2 20 13 5.3 0.69 129 21 6 14
Tier 3 23 12 5.0 0.95 114 21 6 14
Tier 4 330 120 49 9.4 114 21 6 14
tier Mean Median Quartile_1 Minimum N_Obs. N_Doi N_Taxa N_Species
tier 1 430,000 48,000 470 10 264 29 7 26
tier 2 640,000 54,000 3,000 65 215 23 7 22
tier 3 620,000 53,000 2,600 65 213 23 7 22
tier 4 2,200,000 240,000 23,000 640 213 23 7 22
tier Mean Median Quartile_1 Minimum N_Obs. N_Doi N_Taxa N_Species
tier 1 7.3 9.1 1.9 0.26 433 26 7 18
tier 2 18 14 7.0 0.26 137 22 6 15
tier 3 20 14 6.6 1.4 118 22 6 15
tier 4 270 120 57 13 118 22 6 15
Tier Mean Median Quartile_1 Minimum N_Obs. N_Doi N_Taxa N_Species
Tier 1 77,000 9,200 870 18 467 45 7 35
Tier 2 220,000 29,000 3,500 50 240 37 7 29
Tier 3 410,000 55,000 7,600 200 199 31 7 28
Tier 4 1,600,000 280,000 57,000 3,000 199 31 7 28
Tier Mean Median Quartile_1 Minimum N_Obs. N_Doi N_Taxa N_Species
Tier 1 4.9 5.2 3.5 1.5 964 57 8 43
Tier 2 3.0 3.5 2.5 1.5 371 50 8 38
Tier 3 2.6 3.1 1.9 0.90 222 44 8 34
Tier 4 19 20 12 5.5 222 44 8 34
Tier Mean Median Quartile_1 Minimum N_Obs. N_Doi N_Taxa N_Species
Tier 1 34,000 9,200 540 30 534 48 8 37
Tier 2 74,000 14,000 1,400 50 295 40 8 31
Tier 3 120,000 21,000 2,400 150 280 35 8 30
Tier 4 610,000 120,000 20,000 1,700 280 35 8 30
Tier Mean Median Quartile_1 Minimum N_Obs. N_Doi N_Taxa N_Species
Tier 1 3.3 3.8 2.5 0.57 1028 62 8 46
Tier 2 2.6 3.4 2.3 0.57 397 55 8 42
Tier 3 2.5 3.1 1.8 0.68 308 51 8 40
Tier 4 16 15 9.8 4.0 308 51 8 40
  #cols_label(allPass = "Risk and Tech Tier Pass",
   #          risk.pass = "Risk Pass; Tech Pass & Fail",
    #         tech.pass = "Risk Pass & Fail; Tech Fail",
     #        allFail = "Risk Pass & Fail; Tech Pass & Fail")